home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / modules / archive.module < prev    next >
Encoding:
Text File  |  2005-04-01  |  8.1 KB  |  256 lines

  1. <?php
  2. // $Id: archive.module,v 1.79 2005/04/01 15:54:58 dries Exp $
  3.  
  4. /**
  5.  * @file
  6.  * Displays a calendar to navigate old content.
  7.  */
  8.  
  9. /**
  10.  * Implementation of hook_help().
  11.  */
  12. function archive_help($section) {
  13.   switch ($section) {
  14.     case 'admin/modules#description':
  15.       return t('Displays a calendar for navigating older content.');
  16.   }
  17. }
  18.  
  19. /**
  20.  * Generates a monthly calendar, for display in the archive block.
  21.  */
  22. function archive_calendar($original = 0) {
  23.   global $user;
  24.   $edit = $_POST['edit'];
  25.  
  26.   // Extract today's date:
  27.   $start_of_today = mktime(0, 0, 0, date('n', time()), date('d', time()), date('Y', time())) + $user->timezone;
  28.   $end_of_today = mktime(23, 59, 59, date('n', time()), date('d', time()), date('Y', time())) + $user->timezone;
  29.  
  30.   // Extract the requested date:
  31.   if ($edit['year'] && $edit['month'] && $edit['day']) {
  32.     $year = $edit['year'];
  33.     $month = $edit['month'];
  34.     $day = $edit['day'];
  35.  
  36.     $requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
  37.   }
  38.   else if (arg(0) == 'archive' && arg(3)) {
  39.     $year = arg(1);
  40.     $month = arg(2);
  41.     $day = arg(3);
  42.  
  43.     $requested = mktime(0, 0, 0, $month, $day, $year) + $user->timezone;
  44.   }
  45.   else {
  46.     $year = date('Y', time());
  47.     $month  = date('n', time());
  48.     $day = date('d', time());
  49.  
  50.     $requested = $end_of_today + $user->timezone;
  51.   }
  52.  
  53.   $start_of_month = mktime(0, 0, 0, $month, 1, $year);
  54.  
  55.   // Extract first day of the month:
  56.   $first = date('w', $start_of_month);
  57.  
  58.   // Extract last day of the month:
  59.   $last = date('t', $start_of_month);
  60.  
  61.   $end_of_month = mktime(23, 59, 59, $month, $last, $year);
  62.  
  63.   $cache = cache_get("archive:calendar:$day-$month-$year");
  64.  
  65.   if (!empty($cache)) {
  66.     return $cache->data;
  67.   }
  68.  
  69.   // Calculate previous and next months dates and check for shorter months (28/30 days)
  70.   $prevmonth = mktime(23, 59, 59, $month - 1, 1, $year);
  71.   $prev = mktime(23, 59, 59, $month - 1, min(date('t', $prevmonth), $day), $year);
  72.   $nextmonth = mktime(23, 59, 59, $month + 1, 1, $year);
  73.   $next = mktime(23, 59, 59, $month + 1, min(date('t', $nextmonth), $day), $year);
  74.  
  75.   $sql = 'SELECT n.nid, n.created FROM {node} n WHERE n.status = 1 AND n.created > %d AND n.created < %d ORDER BY n.created';
  76.   $sql = db_rewrite_sql($sql);
  77.   $result = db_query($sql, $start_of_month, $end_of_month);
  78.  
  79.   $days_with_posts = array();
  80.   while ($day_with_post = db_fetch_object($result)) {
  81.     $daynum = date('j', $day_with_post->created + $user->timezone);
  82.     if (isset($days_with_posts[$daynum])) {
  83.       $days_with_posts[$daynum]++;
  84.     }
  85.     else {
  86.       $days_with_posts[$daynum] = 1;
  87.     }
  88.   }
  89.  
  90.   // Generate calendar header:
  91.   $output .= "\n<!-- calendar -->\n";
  92.   $output .= '<div class="calendar">';
  93.   $output .= '<table summary="'. t('A calendar to browse the archives') .".\">\n";
  94.   $output .= ' <caption>'. l('┬½', 'archive/'. date('Y/m/d', $prev), array('title' => t('Previous month'))) .' '. format_date($requested, 'custom', 'F') . date(' Y', $requested) .' '. ($nextmonth <= time() ? l('┬╗', 'archive/'. date('Y/m/d', $next), array('title' => t('Next month'))) : '┬á') ."</caption>\n";
  95.  
  96.   // First day of week (0 => Sunday, 1 => Monday, ...)
  97.   $weekstart = variable_get('date_first_day', 0);
  98.  
  99.   // Last day of week
  100.   ($weekstart - 1 == -1) ? $lastday = 6 : $lastday = $weekstart - 1;
  101.  
  102.   // Generate the days of the week:
  103.   $firstcolumn = mktime(0, 0, 0, 3, 20 + $weekstart, 1994);
  104.  
  105.   $output .= " <tr class=\"header-week\">\n";
  106.   $days = array(t('Sunday') => t('Su'), t('Monday') => t('Mo'), t('Tuesday') => t('Tu'), t('Wednesday') => t('We'), t('Thursday') => t('Th'), t('Friday') => t('Fr'), t('Saturday') => t('Sa'));
  107.   if ($weekstart) {
  108.     $days = array_merge(array_slice($days, $weekstart), array_slice($days, 0, $weekstart));
  109.   }
  110.  
  111.   foreach ($days as $fullname => $name) {
  112.     $output .= ' <th abbr="'. $fullname .'">'. $name . "</th>\n";
  113.   }
  114.   $output .= "</tr>\n";
  115.  
  116.   // Initialize temporary variables:
  117.   $nday = 1;
  118.   $sday = $first;
  119.  
  120.   // Loop through all the days of the month:
  121.   while ($nday <= $last) {
  122.     // Set up blank days for first week of the month (allowing individual blank day styling):
  123.     if ($first != $weekstart) {
  124.       $blankdays = ($first - $weekstart + 7) % 7;
  125.       $output .= " <tr class=\"row-week\">" . str_repeat("<td class=\"day-blank\"> </td>\n", $blankdays);
  126.       $first = $weekstart;
  127.     }
  128.     // Start every week on a new line:
  129.     if ($sday == $weekstart) {
  130.       $output .= " <tr class=\"row-week\">\n";
  131.     }
  132.  
  133.     // Print one cell:
  134.     $date = mktime(0, 0, 0, $month, $nday, $year) + $user->timezone;
  135.     if (isset($days_with_posts[$nday])) {
  136.       $daytext = l($nday, "archive/$year/$month/$nday", array("title" => format_plural($days_with_posts[$nday], "1 post", "%count posts")));
  137.       $dayclass = 'day-link';
  138.     }
  139.     else {
  140.       $daytext = $nday;
  141.       $dayclass = 'day-normal';
  142.     }
  143.     if ($date == $requested) {
  144.       $output .= "  <td class=\"day-selected\">$daytext</td>\n";
  145.     }
  146.     else if ($date == $start_of_today) {
  147.       $output .= "  <td class=\"day-today\">$daytext</td>\n";
  148.     }
  149.     else if ($date > $end_of_today) {
  150.       $output .= "  <td class=\"day-future\">$daytext</td>\n";
  151.     }
  152.     else {
  153.       $output .= "  <td class=\"$dayclass\">$daytext</td>\n";
  154.     }
  155.  
  156.     // Start every week on a new line:
  157.     if ($sday == $lastday) {
  158.       $output .=  " </tr>\n";
  159.     }
  160.  
  161.     // Update temporary variables:
  162.     $sday++;
  163.     $sday = $sday % 7;
  164.     $nday++;
  165.   }
  166.  
  167.   // Complete the calendar (allowing individual blank day styling):
  168.   if ($sday != $weekstart) {
  169.     $end = (7 - $sday + $weekstart) % 7;
  170.     $output .= str_repeat("<td class=\"day-blank\"> </td>\n", $end) . "</tr>\n";
  171.   }
  172.  
  173.   $output .= "</table></div>\n\n";
  174.  
  175.   cache_set("archive:calendar:$day-$month-$year", $output, CACHE_TEMPORARY);
  176.  
  177.   return $output;
  178. }
  179.  
  180. /**
  181.  * Implementation of hook_block().
  182.  *
  183.  * Generates a calendar for the current month, with links to the archives
  184.  * for each day.
  185.  */
  186. function archive_block($op = 'list', $delta = 0) {
  187.   if ($op == 'list') {
  188.     $blocks[0]['info'] = t('Calendar to browse archives');
  189.     return $blocks;
  190.   }
  191.   else if ($op == 'view' && user_access('access content')) {
  192.     $block['subject'] = t('Browse archives');
  193.     $block['content'] = archive_calendar();
  194.     return $block;
  195.   }
  196. }
  197.  
  198. /**
  199.  * Implementation of hook_menu().
  200.  */
  201. function archive_menu($may_cache) {
  202.   $items = array();
  203.  
  204.   if ($may_cache) {
  205.     $items[] = array('path' => 'archive', 'title' => t('archives'),
  206.       'access' => user_access('access content'),
  207.       'callback' => 'archive_page',
  208.       'type' => MENU_SUGGESTED_ITEM);
  209.   }
  210.   return $items;
  211. }
  212.  
  213. /**
  214.  * Menu callback; lists all nodes posted on a given date.
  215.  */
  216. function archive_page($year = 0, $month = 0, $day = 0) {
  217.   global $user;
  218.  
  219.   $output = '';
  220.   $op = $_POST['op'];
  221.   $edit = $_POST['edit'];
  222.  
  223.   if ($op == t('Show')) {
  224.     $year = $edit['year'];
  225.     $month = $edit['month'];
  226.     $day = $edit['day'];
  227.   }
  228.  
  229.   $date = mktime(0, 0, 0, $month, $day, $year) - $user->timezone;
  230.   $date_end = mktime(0, 0, 0, $month, $day + 1, $year) - $user->timezone;
  231.  
  232.   // Prepare the values of the form fields.
  233.   $years = drupal_map_assoc(range(2000, 2005));
  234.   $months = array(1 => t('January'), 2 => t('February'), 3 => t('March'), 4 => t('April'), 5 => t('May'), 6 => t('June'), 7 => t('July'), 8 => t('August'), 9 => t('September'), 10 => t('October'), 11 => t('November'), 12 => t('December'));
  235.   $days = drupal_map_assoc(range(0, 31));
  236.  
  237.   $start = '<div class="container-inline">';
  238.   $start .= form_select('', 'year', ($year ? $year : date('Y')), $years). form_select('', 'month', ($month ? $month : date('m')), $months) . form_select('', 'day', ($day ? $day : date('d')), $days) . form_submit(t('Show'));
  239.   $start .= '</div>';
  240.   $output .= form($start);
  241.  
  242.   if ($year && $month && $day) {
  243.     // Fetch nodes for the selected date, if one was specified.
  244.     $sql = 'SELECT n.nid, n.created FROM {node} n WHERE n.status = 1 AND n.created > %d AND n.created < %d ORDER BY n.created';
  245.     $sql = db_rewrite_sql($sql);
  246.     $result = db_query_range($sql, $date, $date_end, 0, 20);
  247.  
  248.     while ($nid = db_fetch_object($result)) {
  249.       $output .= node_view(node_load(array('nid' => $nid->nid)), 1);
  250.     }
  251.   }
  252.   print theme('page', $output);
  253. }
  254.  
  255. ?>
  256.